package org.scboy.common.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;

import org.scboy.common.request.Result;
import org.scboy.common.service.DynamicService;
import org.scboy.utils.entitys.coobi.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import jakarta.servlet.http.HttpServletRequest;

/**
 * 测试类
 */
@Configuration
@RestController
@RequestMapping("/dynamic/test")
public class DynamicTestController {

	Log log=LogFactory.get();
	
	@Autowired
	DynamicService service;
	
	/**
	 * 动态服务测试
	 * @return
	 */
	@RequestMapping("/request/{code}")
	public Result requestTest(@PathVariable("code")String code,HttpServletRequest request){
		Model params=Model.create();
		
		try {
			String _body=getPostData(request);
			if(StrUtil.isNotEmpty(_body)) {
				JSON josn=JSONUtil.parse(_body);
				Model body=josn.toBean(Model.class);
				params.add("body", body);
			}else {
				params.add("body", Model.create());
			}
		} catch (Exception e) {
			log.warn(e);
			params.add("body", Model.create());
		}
		
		Model headers=Model.create();
		Enumeration<String> enumeration = request.getHeaderNames();
		while (enumeration.hasMoreElements()) {
			String name = enumeration.nextElement();
			String value = request.getHeader(name);
			headers.put(name, value);
		}
		params.add("header", headers);
		
		Model _params=Model.create();
		Enumeration<String> paramNames = request.getParameterNames();

		while(paramNames.hasMoreElements()) {
           String name = (String)paramNames.nextElement();
           String value = request.getParameter(name);
           _params.add(name, value);
       	}
		params.add("params", _params);
		String msg=service.dynamicRequestExeTest(code, params);
		return Result.Success(msg,0L);
	}

	@PostMapping("/service/{code}")
	public Result serviceTest(@PathVariable("code")String code,HttpServletRequest request){
		Model params=Model.create();
		try {
			String _body=getPostData(request);
			if(StrUtil.isNotEmpty(_body)) {
				JSON josn=JSONUtil.parse(_body);
				params=josn.toBean(Model.class);
			}
		} catch (Exception e) {
			log.warn(e);
		}

		String msg=service.dynamicServiceExeTest(code, params);
		return Result.Success(msg,0L);
	}
	
	public String getPostData(HttpServletRequest request) {
        StringBuilder data = new StringBuilder();
        String line;
        BufferedReader reader;
        try {
            reader = request.getReader();
            while (null != (line = reader.readLine())) {
                data.append(line);
            }
        } catch (IOException e) {
            return null;
        }
        return data.toString();
    }
}
